package org.example;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

/**
 *  为了验证shellUtil
 */
public class ShellUtil {

    public static void main(String[] args) throws Exception {
        String shellCmd = "";
        System.out.println("main方法开始了");
        if (args.length > 0) {
            shellCmd = args[0];
            System.out.println("进入if判断了,shellCmd" + shellCmd);
            runShell(shellCmd);
        } else {
            System.out.println("进入else判断了,shellCmd" + shellCmd);
            shellCmd = "split -C 80 /root/shellUtil/dataFile /root/shellUtil/dataFile";
            runShell(shellCmd);
        }

        System.out.println("main方法执行完成");
    }


    public static List<String> runShell(String shStr) throws Exception {
        List<String> strList = new ArrayList();
        Process process;
        System.out.println();
        System.out.println("before sh");
        process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shStr}, null, null);
        System.out.println("after sh");
        InputStreamReader ir = new InputStreamReader(process
                .getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        process.waitFor();
        while ((line = input.readLine()) != null) {
            strList.add(line);
            System.out.println("cat_input:" + line);
        }
        return strList;
    }
}
